<template>
  <div class="app-container">
    <!--    <el-form :inline="true" :model="searchFrom" class="demo-form-inline" size="small">-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.number" placeholder="商品编号"/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.name" placeholder="商品名称"/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-button type="primary" @click="onSearch">查询</el-button>-->
    <!--      </el-form-item>-->
    <!--    </el-form>-->
    <el-row class="tool">
      <el-col :span="24">
        <el-button type="primary" size="mini" @click="create()">新增商品</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="scope">
          {{ scope.row.productId }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.productName }}
        </template>
      </el-table-column>
      <el-table-column label="商品主图" align="center" width="120">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.mainPic"
            style="width: 60px; height: 60px"
            :src="scope.row.goodImage[0]"
            :preview-src-list="scope.row.goodImage"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="款号" align="center">
        <template slot-scope="scope">
          {{ scope.row.brandName }}
        </template>
      </el-table-column>
      <el-table-column label="分类信息" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.parentName + ' / ' + scope.row.categoryName }}
        </template>
      </el-table-column>
      <el-table-column label="批发价" align="center">
        <template slot-scope="scope">
          ￥{{ scope.row.productPrice / 100 }}
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center" width="130">
        <template slot-scope="scope">
          <el-tag type="primary" size="small">{{ scope.row.productStock }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="今日基础销量" align="center" width="130">
        <template slot-scope="scope">
          <el-tag type="primary" size="small">{{ scope.row.productSales }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="规格信息" align="center">
        <template slot-scope="scope">
          <el-popover placement="bottom" width="600" trigger="click">
            <el-table :data="scope.row.sku">
              <el-table-column label="颜色" align="center">
                <template slot-scope="scopeItem">
                  {{ scopeItem.row.skuColour }}
                </template>
              </el-table-column>
              <el-table-column label="尺寸" align="center">
                <template slot-scope="scopeItem">
                  {{ scopeItem.row.skuSize }}
                </template>
              </el-table-column>
              <el-table-column label="销量" align="center">
                <template slot-scope="scopeItem">
                  {{ scopeItem.row.sale }}
                </template>
              </el-table-column>
              <el-table-column label="库存" align="center">
                <template slot-scope="scopeItem">
                  {{ scopeItem.row.stock }}
                </template>
              </el-table-column>
              <el-table-column label="预警库存" align="center">
                <template slot-scope="scopeItem">
                  {{ scopeItem.row.lowStock }}
                </template>
              </el-table-column>
            </el-table>
            <el-button slot="reference" size="mini">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="基本操作" width="250">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="salesEdit(scope.row)">销量</el-button>
          <el-button type="primary" size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteProduct(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-pagination">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevChange"
        @next-click="handleNextChange"
      />
    </div>
  </div>
</template>

<script>

import { deleteProduct, getProductPage, updateProduct, updateProductItem } from '@/api/Product'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      fileList: [],
      uploadImgAction: 'http://backstage.manniukeji.com/backend/v1/pmsProductLibrary/excelPmsProductLibrary',
      searchFrom: {
        name: '',
        number: ''
      },
      list: null,
      listLoading: true,
      total: 0,
      limit: 10,
      page: 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const searchParams = this.searchFrom
      searchParams['page'] = this.page
      searchParams['limit'] = this.limit
      getProductPage(searchParams).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    onSearch() {
      this.fetchData()
    },
    /* 上一页*/
    handlePrevChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 下一页*/
    handleNextChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 数字点击*/
    handleCurrentChange(event) {
      this.page = event
      this.fetchData()
    },
    handleSizeChange(event) {
      this.limit = event
      this.fetchData()
    },
    edit(event) {
      this.$router.push({
        path: '/product/create',
        query: {
          productId: event.productId
        }
      })
    },
    deleteProduct(event) {
      this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProduct({ ids: event.productId }).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          } else {
            this.$message.error('网络错误')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    create() {
      this.$router.push('/product/create')
    },
    beforePictureUpload(file) {
      const typeItem = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
      const isJPG = typeItem.indexOf(file.type)
      const isLt2M = file.size / 1024 / 1024 < 20
      if (isJPG < 0) {
        this.$message.error('上传文件只能是 excel 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 20MB!')
      }
      return isJPG && isLt2M
    },
    /* 上传成功*/
    handlePictureSuccess(res, file, fileList) {
      // this.fileList = [{
      //   name: res.data,
      //   url: res.data
      // }]
      this.$message.success('导入成功')
      this.dialogTableVisible = false
      this.fetchData()
    },
    batchExcel() {
      this.dialogTableVisible = true
    },
    template() {
      const link = document.createElement('a')
      link.setAttribute('download', 'http://backstage.manniukeji.com/static/product-template.xlsx')
      link.href = 'http://backstage.manniukeji.com/static/product-template.xlsx'
      link.click()
    },
    salesEdit(event) {
      this.$prompt('请输入销量', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'number'
      }).then(({ value }) => {
        const form = {
          productId: event.productId,
          productSales: value
        }
        updateProductItem(form).then(res => {
          if (res.code === 200) {
            this.$message.success('修改成功')
            this.fetchData()
          } else {
            this.$message.error('修改失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    }
  }
}
</script>
